<template>
  <Teleport to="body">
    <Transition name="modal">
      <div v-if="visible" class="hjr-modal-overlay" @click="handleOverlayClick">
        <div class="hjr-modal" @click.stop>
          <div class="hjr-modal__header">
            <h3 class="hjr-modal__title">{{ title }}</h3>
            <button v-if="showClose" class="hjr-modal__close" @click="handleClose">
              ×
            </button>
          </div>
          <div class="hjr-modal__body">
            <slot />
          </div>
          <div v-if="$slots.footer" class="hjr-modal__footer">
            <slot name="footer" />
          </div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<script>
export default {
  name: 'HjrModal',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    showClose: {
      type: Boolean,
      default: true
    },
    closeOnClickOverlay: {
      type: Boolean,
      default: true
    }
  },
  emits: ['update:visible', 'close'],
  methods: {
    handleClose() {
      this.$emit('update:visible', false)
      this.$emit('close')
    },
    handleOverlayClick() {
      if (this.closeOnClickOverlay) {
        this.handleClose()
      }
    }
  }
}
</script>

<style scoped>
.hjr-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.hjr-modal {
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  max-width: 90%;
  max-height: 90%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.hjr-modal__header {
  padding: 20px 24px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.hjr-modal__title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.hjr-modal__close {
  background: none;
  border: none;
  font-size: 24px;
  color: #999;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.2s;
}

.hjr-modal__close:hover {
  background-color: #f5f5f5;
  color: #666;
}

.hjr-modal__body {
  padding: 20px 24px;
  flex: 1;
  overflow-y: auto;
}

.hjr-modal__footer {
  padding: 0 24px 20px;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 动画 */
.modal-enter-active,
.modal-leave-active {
  transition: all 0.3s ease;
}

.modal-enter-from {
  opacity: 0;
  transform: scale(0.9);
}

.modal-leave-to {
  opacity: 0;
  transform: scale(0.9);
}
</style>
